package com.hlkj.warboot.configurer.util;

import com.alibaba.fastjson.JSONObject;
import com.hlkj.warboot.configurer.util.excel.admin.ReadAdminEntity;
import com.hlkj.warboot.module_sys.entity.AdminEntity;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: TODO 实体类拷贝
 * @Author: lixing
 * @Date: 2020/12/5 10:19
 */
public class ConvertUtils {
    public static void main(String[] args) {
//        ReadAdminEntity readAdminEntity = new ReadAdminEntity(new Date(), "account", 28);
//        AdminEntity adminEntity = convert(readAdminEntity,AdminEntity.class);
//        System.out.println(JSONObject.toJSONString(readAdminEntity));
//        System.out.println(JSONObject.toJSONString(adminEntity));
    }

    /*
     * @Description 将source实体类拷贝到targetClass类型的实体类并返回该实体类
     * @Author lixing
     * @Date 2020/12/5 10:27
     * @Param [source, targetClass] 
     * @return E
     */
    public static <T, E> E convert(T source, Class<E> targetClass) {
        E target = null;
        try {
            target = targetClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
            return null;
        }
        BeanUtils.copyProperties(source, target);
        return target;
    }

    /*
     * @Description 将sourceList集合拷贝到targetClass类型的集合并返回该集合
     * @Author lixing
     * @Date 2020/12/5 10:27
     * @Param [source, targetClass]
     * @return E
     */
    public static <T, E> List<E> convertList(List<T> sourceList, Class<E> targetClass) {
        List<E> targetList = new ArrayList<>();
        for (T source : sourceList) {
            E target = convert(source, targetClass);
            targetList.add(target);
        }
        return targetList;
    }

}
